package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 添加购物车
    * */
    @Override
    public void addGoods(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前登陆用户的id
        Long userCurrentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userCurrentId);

        //动态查询购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectAllInfo(shoppingCart);
        //判断商品是否存在
        if(shoppingCartList != null && !shoppingCartList.isEmpty()) {
            ShoppingCart cart = shoppingCartList.get(0);

            //存在就将其数量加一
            cart.setNumber(cart.getNumber() + 1);
            //修改购物车的数据
            shoppingCartMapper.updateShoppingCart(cart);
        }else {
            //不存在就向购物车里插入一条新数据
            //判断当前查询的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                Dish dish = dishMapper.selectDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //向数据库里插入这条购物车数据
            shoppingCartMapper.insertShopppingCart(shoppingCart);
        }
    }

    /*
    * 查看购物车
    * */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前登陆用户的id
        Long userCurrentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userCurrentId);

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectAllInfo(shoppingCart);
        return shoppingCartList;
    }

    /*
    * 清空购物车
    * */
    @Override
    public void cleanShoppingCart() {
        //获取当前登陆用户的id
        Long userCurrentId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteAllShoppingCart(userCurrentId);
    }

    /*
    * 删除单条购物车数据
    * */
    @Override
    public void deleteSingleShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //封装用于查询和删除的购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userCurrentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userCurrentId);

        //查询购物车这条数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectAllInfo(shoppingCart);

        if(shoppingCartList != null && !shoppingCartList.isEmpty()) {
            ShoppingCart cart = shoppingCartList.get(0);
            //判断number是否为1
            if(cart.getNumber() == 1) {
                //等于1就直接删除整条数据
                //判断当前为菜品还是套餐
                Long dishId = shoppingCartDTO.getDishId();
                if(dishId != null){
                    //当前选择为菜品
                    Dish dish = dishMapper.selectDishById(dishId);
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setImage(dish.getImage());
                    shoppingCart.setAmount(dish.getPrice());

                    shoppingCartMapper.deleteSingleShoppingCart(shoppingCart);
                }else{
                    //当前选择为套餐
                    Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setImage(setmeal.getImage());
                    shoppingCart.setAmount(setmeal.getPrice());

                    shoppingCartMapper.deleteSingleShoppingCart(shoppingCart);
                }
            }else if(cart.getNumber() > 1){
                //大于1就将number减1
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateShoppingCart(cart);
            }
        }
    }
}
